﻿using Asp.Versioning;
using FlyingEye.DeviceServices;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace FlyingEye.Controllers
{
    /// <summary>
    /// 设备配置控制器
    /// </summary>
    [Authorize]
    [ControllerName("DeviceConfiguration")]
    [ApiVersion("1.0")]
    [Route("api/device-app-service/v{version:apiVersion}/device-configurations")]
    public class DeviceConfigurationController : DefaultController
    {

        private readonly DeviceConfigurationService _deviceConfigurationService;

        public DeviceConfigurationController(
            DeviceConfigurationService deviceConfigurationService)
        {
            _deviceConfigurationService = deviceConfigurationService;
        }

        /// <summary>
        /// 插入一个设备配置节点
        /// </summary>
        [HttpPost]
        public async Task<ActionResult<DeviceConfigurationResponse>> InsertAsync([FromBody] CreateDeviceConfigurationRequest configuration)
        {
            return await this.InsertInvokeAsync(async () =>
            {
                return await _deviceConfigurationService.InsertAsync(configuration);
            }, "", e => new { id = e.Id });
        }

        /// <summary>
        /// 删除一个设备配置节点
        /// </summary>
        [HttpDelete("{id}")]
        public async Task<NoContentResult> DeleteAsync(Guid id)
        {
            return await this.DeleteInvokeAsync(async () =>
            {
                await _deviceConfigurationService.DeleteAsync(id);
            });
        }

        /// <summary>
        /// 获取设备节点配置
        /// </summary>
        [HttpGet("{id}")]
        public async Task<ActionResult<DeviceConfigurationResponse>> GetAsync(Guid id)
        {
            return await this.GetInvokeAsync(async () =>
            {
                return await _deviceConfigurationService.GetAsync(id);
            });
        }

        /// <summary>
        /// 更新一个配置
        /// </summary>
        [HttpPut]
        public async Task<ActionResult<DeviceConfigurationResponse>> UpdateAsync([FromBody] UpdateDeviceConfigurationRequest configuration)
        {
            return await this.PutInvokeAsync(async () =>
            {
                return await _deviceConfigurationService.UpdateAsync(configuration);
            });
        }

        /// <summary>
        /// 获取设备节点配置
        /// </summary>
        [HttpGet()]
        public async Task<ActionResult<List<DeviceConfigurationResponse>>> GetListAsync()
        {
            return await this.GetInvokeAsync(async () =>
            {
                return await _deviceConfigurationService.GetListAsync();
            });
        }
    }
}
